#pragma once
#include <iostream>
#include <pthread.h>

namespace LockModule
{
    class Mutex
    {
    public:
        // 锁不允许拷贝和赋值
        Mutex(const Mutex&) = delete;
        const Mutex& operator=(const Mutex&) = delete;

        Mutex()
        {
            int n = ::pthread_mutex_init(&_mutex, nullptr);
            (void)n;
        }
        ~Mutex()
        {
            int n = ::pthread_mutex_destroy(&_mutex);
            (void)n;
        }
        void Lock()
        {
            int n = ::pthread_mutex_lock(&_mutex);
            (void)n;
        }
        pthread_mutex_t* MutexPtr()
        {
            return &_mutex;
        }
        void Unlock()
        {
            int n = ::pthread_mutex_unlock(&_mutex);
            (void)n;
        }

    private:
        pthread_mutex_t _mutex;
    };

    class LockGuard
    {
    public:
        LockGuard(Mutex& mutex)
            :_mutex(mutex)
        {
            _mutex.Lock();
        }
        ~LockGuard()
        {
            _mutex.Unlock();
        }
    private:
        Mutex& _mutex;
    };
}